package com.dc.bobaoge.project.v2.to.v3.business.core.util;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dc.bobaoge.project.base.core.old.entity.OldCoreAdvFictitious;
import com.dc.bobaoge.project.base.core.old.entity.OldCoreFictitious;
import com.dc.bobaoge.project.base.core.old.mapper.OldCoreAdvFictitiousMapper;
import com.dc.bobaoge.project.base.core.old.mapper.OldCoreFictitiousMapper;
import com.dc.bobaoge.project.base.core.util.WalletUtil;
import com.own.business.wallet.mapper.WalletMapper;
import com.own.constant.ConstantNumber;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * FictitiousUtil
 *
 * @author chenxueli
 * @date 2024-01-17 13:34:00
 */
@Slf4j
@Component
public class FictitiousUtil {

    @Resource
    private OldCoreFictitiousMapper oldCoreFictitiousMapper;
    @Resource
    private OldCoreAdvFictitiousMapper oldCoreAdvFictitiousMapper;

    @Resource
    private WalletMapper walletMapper;

    @Resource
    private WalletUtil walletUtil;

    /**
     * 根据用户id设置余额信息
     *
     * @param userId 用户id
     */
    public void init(Long userId) {
        // 分别查询两个钱包的余额信息
        var balance = 0;
        {
            var wrapper = new LambdaQueryWrapper<OldCoreAdvFictitious>().eq(OldCoreAdvFictitious::getUserId, userId).last("limit 1");
            var wallet = oldCoreAdvFictitiousMapper.selectOne(wrapper);
            if (wallet != null && wallet.getFictitiousTotal() != null) {
                balance += BigDecimal.valueOf(wallet.getFictitiousTotal()).multiply(ConstantNumber.BIG_DECIMAL_HUNDRED).intValue();
            }
        }
        {
            var wrapper = new LambdaQueryWrapper<OldCoreFictitious>().eq(OldCoreFictitious::getBcXuniUserId, userId).last("limit 1");
            var wallet = oldCoreFictitiousMapper.selectOne(wrapper);
            if (wallet != null && wallet.getBcXuniMoney() != null) {
                balance += BigDecimal.valueOf(wallet.getBcXuniMoney()).multiply(ConstantNumber.BIG_DECIMAL_HUNDRED).intValue();
            }
        }
        // 初始化钱包
        var wallet = walletUtil.checkAndInit(userId, null, balance);
        if (wallet.getBalance().equals(balance)) {
            return;
        }
        wallet.setBalance(balance);
        walletMapper.updateById(wallet);
    }

}
